package gameObjects;

import java.util.ArrayList;
import java.util.Collections;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;

public class CardStack extends Actor {

	private ArrayList<Card> cardList = new ArrayList<Card>();
	private ArrayList<Card> stackList = new ArrayList<Card>();
	private Texture texture = new Texture(
			Gdx.files.internal("data/cardmap.jpg"));
	private TextureRegion region = new TextureRegion(texture, 0, 0, 575, 450);
	private float cardWidth = 575.0f / 8.0f;
	private float cardHeight = 450.0f / 4.0f;
	private SpriteBatch batch;
	private Sprite cardBack;
	private CardTable table;

	public CardStack(SpriteBatch _batch, CardTable _table) {
		table = _table;
		cardBack = table.getCardBack();

		batch = _batch;
		if (cardList.size() == 0) {
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			loadCards();
		}
		this.setOrigin(0, 0);
		this.setPosition((480 / 2) - cardWidth - 10, (800 / 2) - cardHeight / 2);
		this.setSize(cardWidth, cardHeight);
		this.setTouchable(Touchable.enabled);
		this.addListener(new InputListener() {

			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				System.out.println("stack down");
				table.getCurrentPlayer().addCard(
						stackList.get(stackList.size() - 1));
				stackList.remove(stackList.size() - 1);
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
//				if (stackList.size() > 0)
					
				System.out.println("stack up");
			}

		});
		cardBack.setOrigin(0, 0);
		cardBack.setPosition(this.getX(), this.getY());
		stackList = cardList;
		Collections.shuffle(stackList);

	}

	public void loadCards() {
		System.out.println("cardwidth: " + cardWidth);
		System.out.println("cardHeight: " + cardHeight);

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 8; j++) {
				Sprite cardSprite = new Sprite();
				cardSprite.setRegion(region, (j) * (int) cardWidth, (i)
						* (int) cardHeight, (int) cardWidth, (int) cardHeight);
				cardSprite.setSize(cardWidth, cardHeight);
				cardSprite.setOrigin(cardWidth / 2, cardHeight / 2);
				cardList.add(new Card(cardSprite,cardBack, batch, i, j,table));
				System.out.println("load card: " + i + j);

			}
		}
	}

	public void reShufflePlayingCards(ArrayList<Card> remainingCards) {
		if (stackList.size() == 0) {
			stackList.addAll(remainingCards);
			Collections.shuffle(stackList);
			Collections.shuffle(stackList);
		}
	}

	public int getStackCount() {

		return stackList.size();
	}

	public Card takeTopCard() {
		if (stackList.size() == 0)
			return null;
		Card card = stackList.get(0);
		stackList.remove(0);
		return card;

	}

	@Override
	public void act(float delta) {
		super.act(delta);

		if (stackList.size() > 0) {

			cardBack.draw(batch);

		}

	}

	private class StackListener extends InputListener {

		@Override
		public boolean touchDown(InputEvent event, float x, float y,
				int pointer, int button) {
			System.out.println("card down");
			return true;
		}

		@Override
		public void touchUp(InputEvent event, float x, float y, int pointer,
				int button) {
		}

		@Override
		public void touchDragged(InputEvent event, float x, float y, int pointer) {

			System.out.println("dragging");
		}

	}

}
